package com.erp.products.addproduct.controller;

import com.erp.common.annotation.Log;
import com.erp.common.config.RuoYiConfig;
import com.erp.common.core.domain.AjaxResult;
import com.erp.common.core.domain.model.LoginUser;
import com.erp.common.enums.BusinessType;
import com.erp.common.utils.StringUtils;
import com.erp.common.utils.oss.AliyunOSSConstants;
import com.erp.common.utils.oss.AliyunOSSUtil;
import com.erp.products.addproduct.domain.ErpProductsPrices;
import com.erp.products.addproduct.service.AddproductService;
import com.erp.products.productlist.domain.ErpProducts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.erp.framework.web.service.TokenService;


import static cn.hutool.core.lang.Console.log;
import static com.erp.common.core.domain.AjaxResult.error;
import static com.erp.common.utils.SecurityUtils.getLoginUser;

@Slf4j
@RestController
@RequestMapping("/product/addproduct")
public class AddproductController {
    @Autowired
    private AddproductService addproductService;
    @Autowired
    private TokenService tokenService;

    //添加商品基础信息
    @RequestMapping("/addProducts")
    public AjaxResult addProducts(ErpProducts erpProducts) {
        addproductService.addProducts(erpProducts);
        // 返回添加结果, 成功后返回商品id
        if (erpProducts.getProductId() == null) {
            return error("添加失败");
        }
        return AjaxResult.success(erpProducts.getProductId());
    }

    //添加商品价格
    @RequestMapping("/addPrice")
    public AjaxResult addPrice(ErpProductsPrices erpProductsPrices) {
        addproductService.addPrice(erpProductsPrices);
        return AjaxResult.success("添加成功");
    }

    //查询 单位  商品类型  商品分类
    @RequestMapping("/listUnit")
    public AjaxResult listUnit() {
        return AjaxResult.success(addproductService.listUnit());
    }

    @RequestMapping("/listProductType")
    public AjaxResult listProductType() {
        return AjaxResult.success(addproductService.listProductType());
    }

    @RequestMapping("/listProductCategory")
    public AjaxResult listCategory() {
        return AjaxResult.success(addproductService.listCategory());
    }

    /**
     * 商品图片上传
     */
    @Log(title = "商品图片", businessType = BusinessType.UPDATE)
    @PostMapping("/upload/image")
    public AjaxResult productImage(@RequestParam("avatarfile") MultipartFile file, @RequestParam("productId") Long productId) throws Exception
    {
        if (!file.isEmpty())
        {
            // 使用自己封装的工具类上传文件到阿里云OSS
            String productImage = AliyunOSSUtil.upload(file);

            System.out.println("成功上传productImage = " + productImage);
            
            // 存储图片路径到数据库，只存储后半部分路径（不带域名）
            boolean b = addproductService.updateProductImage(productId, productImage);
            if (b)
            {
                AjaxResult ajax = AjaxResult.success();
                // 返回给前端完整的图片URL（包含域名）
                ajax.put("imgUrl", AliyunOSSConstants.COMMON_URL + productImage);
                return ajax;
            }
        }
        return error("上传图片异常，请联系管理员");
    }

}